using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Threading;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Net;
using System.Configuration;
using System.Data.SqlClient;


namespace NetMonitor.Things
{
    
    class AxUsersThing : InfoThing
    {
        protected override int ping()
        {    
            SqlConnection myConnection = new SqlConnection(
                ConfigurationManager.ConnectionStrings["AxLiveConnectionString"].ConnectionString
                );
            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                e.ToString();
                return -13;
            }
            Int32 iRet;
            SqlCommand myCommand = new SqlCommand("select count(sessionid) from dbo.SYSCLIENTSESSIONS where status = 1 AND clientType = 0", myConnection);
            iRet = (Int32)myCommand.ExecuteScalar();
            myConnection.Close();
            return (int)iRet;
        }
    }



    class AtlasUsersThing : InfoThing
    {
        protected override int ping()
        {
            SqlConnection myConnection = new SqlConnection(
                ConfigurationManager.ConnectionStrings["AxLiveConnectionString"].ConnectionString);
            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                e.ToString();
                return -13;
            }
            Int32 iRet;
            SqlCommand myCommand = new SqlCommand("select count(userid) from sysLastValue where ElementName = 'Atlas35User' ", myConnection);
            iRet = (Int32)myCommand.ExecuteScalar();
            myConnection.Close();
            return (int)iRet;
        }
    }




    class BatchProcessorThing : InfoThing
    {
        protected override int ping()
        {
            SqlConnection myConnection = new SqlConnection(
                ConfigurationManager.ConnectionStrings["AxLiveConnectionString"].ConnectionString);
            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                e.ToString();
                return -13;
            }
            
            SqlCommand myCommand = new SqlCommand("select LASTRUNDATE,  LASTRUNTIME FROM dbo.IMCOSTINGCONTROL where dataareaid = 'rov'", myConnection);
 
            SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            
            while (myReader.Read())
            {

                DateTime dt = DateTime.Now.AddSeconds(-myReader.GetInt32(1));
                return dt.Hour * 60 * 60 + dt.Minute * 60 + dt.Second;
            }
            return -31;
        }
    }




    class AxRovOrdersThing : InfoThing
    {
        public string Company = "NEN";

        protected override void init()
        {
            classDescription = "Gets number of Ax Orders for ROV company";
        }

        protected override int ping()
        {
            SqlConnection myConnection = new SqlConnection(
                ConfigurationManager.ConnectionStrings["AxLiveConnectionString"].ConnectionString);
            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                e.ToString();
                return -13;
            }
            Int32 iRet;
            SqlCommand myCommand = new SqlCommand(
                @"select count(*) from dbo.SALESTABLE where 
	                DeliveryDate >= DATEADD(day, DATEDIFF(day, 0, GETDATE()), 1)
	                AND DeliveryDate < DATEADD(day, DATEDIFF(day, 0, GETDATE()), 2)
	                AND SalesStatus = 1
	                AND ICSSALESPICKINGTYPE != 3
	                AND DataAreaId = '" + Company + "'"
                , myConnection);
            iRet = (Int32)myCommand.ExecuteScalar();
            myConnection.Close();
            return (int)iRet;
        }
    }





    class FreeSpaceThing : InfoThing
    {
        public string Drive = "C";

        protected override void init()
        {
            classDescription = "Gets the free diskspace of a drive";
        }

        protected override int ping()
        {
            string s = "FS" + Drive;
            Byte[] data = System.Text.Encoding.ASCII.GetBytes(s);
            Byte[] reply = new Byte[512];
            try
            {
                TcpClient client = new TcpClient(hostname, 9000);
                if (!client.Connected)
                    return -3;
                client.Client.SendTimeout = timeOut;
                client.Client.Send(data, SocketFlags.None);
                client.Client.ReceiveTimeout = timeOut;
                if (client.Client.Receive(reply, SocketFlags.None) <= 0)
                    return -4;

                client.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
                return -1;
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
                return -2;
            }
            int iRet = -6;
            int.TryParse(Encoding.ASCII.GetString(reply), out iRet);

            return iRet;
        }
    }

    class StockMovementsPerHrThing : InfoThing
    {
        public int Seconds = 3600;
        protected override void init()
        {
            classDescription = "Stock Movements per hour";
            alias = "Movements/Hr";      
        }

        protected override int ping()
        {
            SqlConnection myConnection = new SqlConnection(
                ConfigurationManager.ConnectionStrings["AxLiveConnectionString"].ConnectionString);
            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                e.ToString();
                return -13;
            }
            Int32 iRet;

            DateTime t = DateTime.Now;
            int ti = t.Hour * 60 * 60 + t.Minute * 60 + t.Second;
            DateTime dt = DateTime.Now.AddSeconds(-Seconds);
            int dti = dt.Hour * 60 * 60 + dt.Minute * 60 + dt.Second;
            string sql = @"select count(RECID) from dbo.INVENTJOURNALTRANS
where TRANSDATE  = dateadd(day, datediff(day, 0, getdate()), 0)
and createdtime 
between " + dti.ToString() + @"
and
" + ti.ToString() + @"
And dataareaid = 'ROV'";
            SqlCommand myCommand = new SqlCommand(sql, myConnection);
            iRet = (Int32)myCommand.ExecuteScalar();
            myConnection.Close();
            return (int)iRet;
        }
    }
}
